package com.definesys.smecrailtransit.module.RealTime.RealTimeAdapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.BR;
import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.LayoutItemBinding;
import com.definesys.smecrailtransit.databinding.LayoutItemFooterBinding;
import com.definesys.smecrailtransit.databinding.LayoutItemHeaderBinding;
import com.definesys.smecrailtransit.module.RealTime.bean.RealTimeBean;
import com.definesys.smecrailtransit.utils.CommonUtils;

import java.util.ArrayList;

/**
 * Created by xupeizuo on 2017/7/20.
 */

public class RealTimeAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {

    private ArrayList<RealTimeBean> timeBeanArrayList=new ArrayList<>();

    private final int VIEW_TYPE_HEADER_INVISIBLE=1001;
    private final int VIEW_TYPE_BODY_VISIBLE=1002;
    private final int VIEW_TYPE_FOOTER_VISIBLE=1003;

    public RealTimeAdapter() {
        timeBeanArrayList.add(new RealTimeBean());
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == VIEW_TYPE_HEADER_INVISIBLE){
            LayoutItemHeaderBinding layoutItemHeaderBinding=DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    ,R.layout.layout_item_header,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutItemHeaderBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutItemHeaderBinding);
            return smecBaseRecyclerViewHolder;
        }
        else if(viewType == VIEW_TYPE_BODY_VISIBLE){
            LayoutItemBinding layoutItemBinding=DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    ,R.layout.layout_item,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutItemBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutItemBinding);
            return smecBaseRecyclerViewHolder;
        }else if(viewType == VIEW_TYPE_FOOTER_VISIBLE){
            LayoutItemFooterBinding layoutItemFooterBinding=DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    ,R.layout.layout_item_footer,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutItemFooterBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutItemFooterBinding);
            return smecBaseRecyclerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        if(holder.getViewType() == VIEW_TYPE_HEADER_INVISIBLE){

        }else if(holder.getViewType() == VIEW_TYPE_BODY_VISIBLE){
            holder.getBinding().setVariable(BR.realViewModel,timeBeanArrayList.get(position));
        }else if(holder.getViewType() == VIEW_TYPE_FOOTER_VISIBLE){

        }
    }

    @Override
    public int getItemCount() {
        return timeBeanArrayList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(timeBeanArrayList!=null&& timeBeanArrayList.size()>0){
            if(position == 0){
                return VIEW_TYPE_HEADER_INVISIBLE;
            }
        }
        return VIEW_TYPE_BODY_VISIBLE;
    }

    public void setTimeBeanArrayList(ArrayList<RealTimeBean> arrayList) {

        if(CommonUtils.notEmpty(arrayList)){
            timeBeanArrayList.clear();
            timeBeanArrayList.addAll(arrayList);
            notifyDataSetChanged();
        }
    }

    /**
     * 在头部开始增加数据
     * @param arrayList
     */
    public void addHeader(ArrayList<RealTimeBean> arrayList) {
        if(CommonUtils.notEmpty(arrayList)){

            ArrayList<RealTimeBean> arrayList1=new ArrayList<>();

            if(CommonUtils.notEmpty(arrayList)){
                for(int t =arrayList.size()-1;t>=0;t--){
                    arrayList1.add(arrayList.get(t));
                }
            }

            for(int i=0;i<arrayList1.size();i++){

                if(timeBeanArrayList.size()>1){
                    for(int j=timeBeanArrayList.size()-1;j>=0;j--){
                        if(arrayList1.get(i).getId() == timeBeanArrayList.get(j).getId()){
                            timeBeanArrayList.remove(timeBeanArrayList.get(j));
                            notifyItemRemoved(j);
                        }
                    }
                }
            }

            for(int i=0;i<arrayList1.size();i++){
                timeBeanArrayList.add(1,arrayList1.get(i));
                notifyItemInserted(1);
            }
        }
    }

    /**
     * 在底部增加数据
     * @param arrayList
     */
    public void addFooter(ArrayList<RealTimeBean> arrayList) {
        if(CommonUtils.notEmpty(arrayList)){

            for(RealTimeBean realTimeBean : arrayList){
                timeBeanArrayList.add(timeBeanArrayList.size(),realTimeBean);
                notifyItemInserted(timeBeanArrayList.size()-1);

            }
        }
    }

    public ArrayList<RealTimeBean> getTimeBeanArrayList() {
        return timeBeanArrayList;
    }

}
